<template>
  <el-dialog title="修改密码" :model-value="modelValue" @update:model-value="$emit('update:modelValue', $event)" width="30%"
    :close-on-click-modal="false" @close="handelCancel">
    <el-form :model="updatePasswordForm" status-icon :rules="rules" ref="updatePasswordFormInstance" label-width="100px"
      size="small">
      <el-form-item label="原密码 :" prop="passwordOld">
        <el-input type="password" v-model="updatePasswordForm.passwordOld" autocomplete="off" />
      </el-form-item>
      <el-form-item label="新密码 :" prop="passwordNew">
        <el-input type="password" v-model="updatePasswordForm.passwordNew" autocomplete="off" />
      </el-form-item>
      <el-form-item label="确认密码 :" prop="passwordDetermine">
        <el-input type="password" v-model="updatePasswordForm.passwordDetermine" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handelCancel" size="small">取 消</el-button>
      <el-button type="primary" @click="updatePasswordBtn" size="small">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { HTTP as SERVICE } from "@/tools";
import { useSystemInfo } from "@/store/pinia-store";
import { computed } from "vue";

export default {
  props: {
    modelValue: Boolean,
  },
  emits: ["update:modelValue"],
  setup() {
    const systemInfo = useSystemInfo();
    const userInfo = computed(() => systemInfo.userInfo);
    return { userInfo };
  },
  data() {
    //输入新密码验证
    let firstPassValidate = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        /*
          // 无效代码
        if (this.updatePasswordForm.passwordDetermine !== "") {
          this.$refs.updatePasswordFormInstance.validateField("passwordDetermine");
        } */
        callback();
      }
    };
    let secondPassValidate = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.updatePasswordForm.passwordNew) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      // 修改密码表单
      updatePasswordForm: {
        passwordOld: "",
        passwordNew: "",
        // 确认密码
        passwordDetermine: "",
      },
      rules: {
        passwordOld: [{ required: true, message: "不能为空", trigger: "blur" }],
        passwordNew: [
          { required: true, validator: firstPassValidate, trigger: "blur" },
        ],
        passwordDetermine: [
          { required: true, validator: secondPassValidate, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    /**
     * 关闭弹窗
     */
    handelCancel() {
      if (!this.modelValue) return;
      const { updatePasswordFormInstance } = this.$refs;
      updatePasswordFormInstance.resetFields();
      this.$emit("update:modelValue", false);
    },
    //修改密码
    updatePasswordBtn() {
      const { updatePasswordFormInstance } = this.$refs;
      updatePasswordFormInstance
        .validate()
        .then(() => {
          const { id } = this.userInfo;
          const { passwordOld: password, passwordNew: newPassword } =
            this.updatePasswordForm;
          const finelPostForm = { password, newPassword, id };
          console.log(finelPostForm);
          SERVICE.post("/sysUser/updatePwd", finelPostForm).then(
            () => {
              this.$message.success({
                message: "修改密码成功！",
                type: "success",
              });
              this.handelCancel();
            },
            (E) => {
              this.$message.warning("修改密码失败！" + E);
            }
          );
        })
        .catch((e) => {
          // this.$message.warning("请检查密码输入！");
          console.error(e);
        });
    },
  },
};
</script>

<style></style>
